package simplenotes;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class MainFrame extends javax.swing.JFrame {

    WaitingWindow waw = new WaitingWindow();

    public MainFrame() {
        initComponents();
        setLocationRelativeTo(null);

        Vars.LoadTaskList();
        RefreshLists();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuItem7 = new javax.swing.JMenuItem();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList4 = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        jMenuItem5.setText("Открыть");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem5);
        jPopupMenu1.add(jSeparator1);

        jMenu3.setText("Перенести...");
        jMenu3.setFont(jMenu3.getFont().deriveFont(jMenu3.getFont().getStyle() & ~java.awt.Font.BOLD));

        jMenuItem12.setFont(jMenuItem12.getFont().deriveFont(jMenuItem12.getFont().getStyle() & ~java.awt.Font.BOLD));
        jMenuItem12.setText("На сегодня");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem12ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem12);

        jMenuItem8.setFont(jMenuItem8.getFont().deriveFont(jMenuItem8.getFont().getStyle() & ~java.awt.Font.BOLD));
        jMenuItem8.setText("На завтра");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem8);

        jMenu4.setText("На следующую неделю...");
        jMenu4.setFont(jMenu4.getFont().deriveFont(jMenu4.getFont().getStyle() & ~java.awt.Font.BOLD));

        jMenuItem9.setFont(jMenuItem9.getFont().deriveFont(jMenuItem9.getFont().getStyle() & ~java.awt.Font.BOLD));
        jMenuItem9.setText("Понедельник");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem9);

        jMenuItem10.setFont(jMenuItem10.getFont().deriveFont(jMenuItem10.getFont().getStyle() & ~java.awt.Font.BOLD));
        jMenuItem10.setText("Ровно через неделю");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem10);

        jMenu3.add(jMenu4);

        jMenuItem11.setFont(jMenuItem11.getFont().deriveFont(jMenuItem11.getFont().getStyle() & ~java.awt.Font.BOLD));
        jMenuItem11.setText("Другое...");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem11);

        jPopupMenu1.add(jMenu3);

        jMenuItem6.setFont(jMenuItem6.getFont().deriveFont(jMenuItem6.getFont().getStyle() & ~java.awt.Font.BOLD));
        jMenuItem6.setText("Сделано!");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem6);
        jPopupMenu1.add(jSeparator2);

        jMenuItem7.setFont(jMenuItem7.getFont().deriveFont(jMenuItem7.getFont().getStyle() & ~java.awt.Font.BOLD));
        jMenuItem7.setText("Удалить");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem7);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(Vars.getVersion());
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jTabbedPane1.setDoubleBuffered(true);

        jScrollPane1.setDoubleBuffered(true);

        jList1.setModel(new DefaultListModel());
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setCellRenderer(new TaskRenderer());
        jList1.setDoubleBuffered(true);
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jLabel4.setText("Фильтр:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 743, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Задачи", jPanel1);

        jList4.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList4.setDoubleBuffered(true);
        jScrollPane4.setViewportView(jList4);

        jLabel5.setText("Фильтр:");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 743, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Контроль", jPanel5);

        jList3.setModel(new DefaultListModel());
        jList3.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList3.setDoubleBuffered(true);
        jScrollPane3.setViewportView(jList3);

        jLabel2.setText("Фильтр:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 743, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Сделанное", jPanel3);

        jLabel1.setText("Контакты будут пожже...");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 763, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addGap(0, 314, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGap(0, 315, Short.MAX_VALUE)))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 367, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addGap(0, 176, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGap(0, 177, Short.MAX_VALUE)))
        );

        jTabbedPane1.addTab("Контакты", jPanel4);

        jButton1.setText("Новая задача...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Новый контакт...");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton4.setText("Обновить");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jMenu1.setText("Программа");

        jMenuItem4.setText("Синхронизация...");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem3.setText("Настройки...");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem2.setText("Выход");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Справка");

        jMenuItem1.setText("О программе...");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 768, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 435, Short.MAX_VALUE)
                        .addComponent(jButton4)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        NewTask dlg = new NewTask(this, true);
        dlg.setLocationRelativeTo(this);
        dlg.setVisible(true);
        if (dlg.getTaskObj() != null) {
            Vars.tasklist.add(dlg.getTaskObj());
            Vars.SaveTaskList();
            RefreshLists();
        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        jList1.setSelectedIndex(jList1.locationToIndex(evt.getPoint()));
        if (jList1.getSelectedIndex() < 0) {
            return;
        }
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            jPopupMenu1.show(jList1, evt.getX(), evt.getY());
            return;
        }
        if (evt.getClickCount() < 2) {
            return;
        }

        TaskObj ta = (TaskObj) jList1.getModel().getElementAt(jList1.getSelectedIndex());
        TaskViewer dlg = new TaskViewer(this, true, ta);
        dlg.setLocationRelativeTo(this);
        dlg.setVisible(true);

        Vars.SaveTaskList();
        RefreshLists();
    }//GEN-LAST:event_jList1MouseClicked

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        OptionsDialog dlg = new OptionsDialog(this, true);
        dlg.setLocationRelativeTo(this);
        dlg.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        dispose();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        if (jList1.getSelectedIndex() < 0) {
            return;
        }
        TaskObj ta = (TaskObj) jList1.getModel().getElementAt(jList1.getSelectedIndex());

        try {
            Socket socket = new Socket("localhost", 32115);
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            pw.println("SyncOne");

            Long l;

            l = ta.getTaskID();
            pw.println(l.toString());

            l = ta.getCreationTime().getTimeInMillis();
            pw.println(l.toString());

            l = ta.getFinishTime().getTimeInMillis();
            pw.println(l.toString());

            pw.println(Vars.getHexString(ta.getTaskText()));

            pw.println(ta.getDone().toString());

            pw.println(ta.getTekuchka().toString());

            l = ta.getChangedTime().getTimeInMillis();
            pw.println(l.toString());

            String answ = br.readLine();

            if (!answ.equals("Ok")) {
                String id = br.readLine();
                String ct = br.readLine();
                String ft = br.readLine();
                String txt = br.readLine();
                String dn = br.readLine();
                String tek = br.readLine();
                String cht = br.readLine();

                TaskObj gta = new TaskObj(id, ct, ft, txt, dn, tek, cht);

                Vars.tasklist.set(Vars.tasklist.indexOf(ta), gta);

                Vars.SaveTaskList();
                RefreshLists();
            }

            pw.close();
            br.close();
            socket.close();
        } catch (UnknownHostException ex) {
            System.out.println(ex.toString());
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            Socket socket = new Socket("localhost", 32115);
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            pw.println("SyncAll");

            for (TaskObj ta : Vars.tasklist) {
                pw.println(ta.getTaskID());
            }
            pw.println("That's all");

            String answ;
            while ((answ = br.readLine()).equals("New")) {
                String id = br.readLine();
                String ct = br.readLine();
                String ft = br.readLine();
                String txt = br.readLine();
                String dn = br.readLine();
                String tek = br.readLine();
                String cht = br.readLine();
                TaskObj gta = new TaskObj(id, ct, ft, txt, dn, tek, cht);
                Vars.tasklist.add(gta);
                Vars.SaveTaskList();
            }

            // Продумать удаление... А то удаленные восстанавливаются при синхронизации :)

            pw.close();
            br.close();
            socket.close();
        } catch (UnknownHostException ex) {
            System.out.println(ex.toString());
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        Vars.LoadTaskList();
        RefreshLists();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        waw.close();
    }//GEN-LAST:event_formWindowOpened

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        TaskObj ta = (TaskObj) jList1.getModel().getElementAt(jList1.getSelectedIndex());

        TaskViewer dlg = new TaskViewer(this, true, ta);
        dlg.setLocationRelativeTo(this);
        dlg.setVisible(true);

        Vars.SaveTaskList();
        RefreshLists();
}//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        TaskObj ta = (TaskObj) jList1.getModel().getElementAt(jList1.getSelectedIndex());
        ta.getFinishTime().add(Calendar.DAY_OF_YEAR, 1);
        ta.setChangedTime(Calendar.getInstance());
        Vars.SaveTaskList();
        RefreshLists();
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        TaskObj ta = (TaskObj) jList1.getModel().getElementAt(jList1.getSelectedIndex());
        ta.setDone(true);
        ta.setChangedTime(Calendar.getInstance());
        Vars.SaveTaskList();
        RefreshLists();
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        if (JOptionPane.showConfirmDialog(null,
                "Вы уверены, что хотите удалить эту запись?",
                "Подтверждение",
                JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) ==
                JOptionPane.YES_OPTION) {
            TaskObj ta = (TaskObj) jList1.getModel().getElementAt(jList1.getSelectedIndex());
            Vars.tasklist.remove(ta);
            RefreshLists();
            Vars.SaveTaskList();
        }
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
        TaskObj ta = (TaskObj) jList1.getModel().getElementAt(jList1.getSelectedIndex());
        ta.getFinishTime().add(Calendar.WEEK_OF_YEAR, 1);
        ta.setChangedTime(Calendar.getInstance());
        Vars.SaveTaskList();
        RefreshLists();
    }//GEN-LAST:event_jMenuItem10ActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
        TaskObj ta = (TaskObj) jList1.getModel().getElementAt(jList1.getSelectedIndex());

        ta.getFinishTime().set(
                Calendar.getInstance().get(Calendar.YEAR),
                Calendar.getInstance().get(Calendar.MONTH),
                Calendar.getInstance().get(Calendar.DAY_OF_MONTH));

        switch (ta.getFinishTime().get(Calendar.DAY_OF_WEEK)) {
            case Calendar.SUNDAY:
                ta.getFinishTime().add(Calendar.DAY_OF_YEAR, 1);
                break;
            case Calendar.MONDAY:
                ta.getFinishTime().add(Calendar.DAY_OF_YEAR, 7);
                break;
            case Calendar.TUESDAY:
                ta.getFinishTime().add(Calendar.DAY_OF_YEAR, 6);
                break;
            case Calendar.WEDNESDAY:
                ta.getFinishTime().add(Calendar.DAY_OF_YEAR, 5);
                break;
            case Calendar.THURSDAY:
                ta.getFinishTime().add(Calendar.DAY_OF_YEAR, 4);
                break;
            case Calendar.FRIDAY:
                ta.getFinishTime().add(Calendar.DAY_OF_YEAR, 3);
                break;
            case Calendar.SATURDAY:
                ta.getFinishTime().add(Calendar.DAY_OF_YEAR, 2);
                break;
        }

        ta.setChangedTime(Calendar.getInstance());

        Vars.SaveTaskList();
        RefreshLists();
    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem12ActionPerformed
        TaskObj ta = (TaskObj) jList1.getModel().getElementAt(jList1.getSelectedIndex());

        ta.getFinishTime().set(
                Calendar.getInstance().get(Calendar.YEAR),
                Calendar.getInstance().get(Calendar.MONTH),
                Calendar.getInstance().get(Calendar.DAY_OF_MONTH));

        ta.setChangedTime(Calendar.getInstance());

        Vars.SaveTaskList();
        RefreshLists();
    }//GEN-LAST:event_jMenuItem12ActionPerformed

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
        TaskObj ta = (TaskObj) jList1.getModel().getElementAt(jList1.getSelectedIndex());
        ChangeFinishDateDialog dlg = new ChangeFinishDateDialog(null, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
        if (dlg.isChanged()) {
            ta.setFinishTime(dlg.getChangedDate());
            ta.setChangedTime(Calendar.getInstance());
            RefreshLists();
            Vars.SaveTaskList();
        }
    }//GEN-LAST:event_jMenuItem11ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        AboutDialog dlg = new AboutDialog(this, true);
        dlg.setLocationRelativeTo(null);
        dlg.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void RefreshLists() {

        Collections.sort(Vars.tasklist, new Comparator<TaskObj>() {

            @Override
            public int compare(TaskObj o1, TaskObj o2) {
                return o1.getFinishTime().compareTo(o2.getFinishTime());
            }
        });

        DefaultListModel lm;
        Calendar cc = Calendar.getInstance();
        cc.set(Calendar.HOUR_OF_DAY, 0);
        cc.set(Calendar.MINUTE, 0);
        cc.set(Calendar.SECOND, 0);
        cc.set(Calendar.MILLISECOND, 0);

        // List 1
        lm = (DefaultListModel) jList1.getModel();
        lm.clear();
        for (TaskObj ta : Vars.tasklist) {
            ta.getFinishTime().set(Calendar.HOUR_OF_DAY, 0);
            ta.getFinishTime().set(Calendar.MINUTE, 0);
            ta.getFinishTime().set(Calendar.SECOND, 0);
            ta.getFinishTime().set(Calendar.MILLISECOND, 0);
            if (!ta.getDone()) {
                lm.addElement(ta);
            }
        }
        jList1.setModel(lm);
        // End of list 1

        // List 3
        lm = (DefaultListModel) jList3.getModel();
        lm.clear();
        for (TaskObj ta : Vars.tasklist) {
            if (ta.getDone()) {
                lm.addElement(ta);
            }
        }
        jList3.setModel(lm);
        // End of list 3

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JList jList1;
    private javax.swing.JList jList3;
    private javax.swing.JList jList4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration//GEN-END:variables
}
